<?php

namespace app\adminmini\service;

use think\facade\Db;

class RenewalorderService extends BaseService
{
    public function getList()
    {
        $data = request()->param();


        $where = [];
        if (isset($data['mobile'])) {
            if ($data['mobile'] != "") {
                $where[] = ['u.mobile', '=', trim($data['mobile'])];
            }
        }
        if (isset($data['mobile'])) {
            if ($data['mobile'] != "") {
                $where[] = ['u.mobile', '=', trim($data['mobile'])];
            }
        }
        if (isset($data['add_time'])) {
            if ($data['add_time'] != "") {
                $where[] = ['o.create_time', '>=', strtotime($data['add_time'])];
            }
        }
        if (isset($data['end_time'])) {
            if ($data['end_time'] != "") {
                $where[] = ['o.create_time', '<=', strtotime($data['end_time']) + 86400];
            }
        }
        if (isset($data['name'])) {
            if ($data['name'] != "") {
                $where[] = ['c.name', '=', trim($data['name'])];
            }
        }
        $where[] = ['o.mark', '=', 1];
        $where[] = ['o.store_id', '=', $this->userId()];
        if (isset($data['pay_type'])) {
            if ($data['pay_type'] != "") {
                $where[] = ['o.pay_type', '=', $data['pay_type']];
            }
        }
        $list = Db::name('customer_renewal_order')
            ->alias('o')
            ->join('mini_room_list m', 'm.id=o.store_id')
            ->join('customer_course c', 'c.id=o.seat_id')
            ->join('member u', 'u.id=o.user_id')->field('o.*,u.nickname,u.mobile,c.name,m.room_name')
            ->where($where)->order('o.create_time desc')->page(PAGE, PERPAGE)
            ->select();

        foreach ($list as $i => $v) {
            $v['id'] = $i + 1;
            $list[$i] = $v;
        }

        $count = Db::name('customer_renewal_order')
            ->alias('o')
            ->join('member u', 'u.id=o.user_id')->field('o.*,u.nickname,u.mobile')
            ->where($where)->order('o.create_time desc')->page(PAGE, PERPAGE)
            ->count();


        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;
    }
}